<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/8
 * Time: 9:17
 */

namespace app\controller\app;

use app\model\app\AppKefuClick;
use app\model\sys\SysConfig;
use think\facade\Db;

class User extends Base
{
    public function info(){
        $config = [];
        $select = SysConfig::select()->toArray();
        $type = input('type',0);
        foreach ($select AS $key => $value) {
            $config[$value['param_key']] = $value['param_value'];
        }
        if ($type == 1) {
            foreach ($this->userInfo->toArray() AS $key => $value) {
                if (is_null($value)) $this->userInfo->$key = '';
            }
            foreach ($this->userInfo->kefu->toArray() AS $key => $value) {
                if (is_null($value)) $this->userInfo->kefu->$key = '';
            }
            if (!empty($this->userInfo->basic->user_id)) {
                foreach ($this->userInfo->basic->toArray() AS $key => $value)
                    if (is_null($value)) $this->userInfo->basic->$key = '';
            }
        }
        return $this->msg(['user' => $this->userInfo,'config' => $config]);
    }
    public function save(){
        $post = input('post.');
        $post['kefu_id'] = $this->userInfo->kefu_id;
        $post['update_time'] = now();
        if(empty($this->userInfo->basic->id))
            $this->userInfo->basic()->save($post);
        else
            $this->userInfo->basic->save($post);
        return $this->msg();
    }


    public function pay_click(){
        try {
            Db::startTrans();
            $model = new AppKefuClick();
            $kefu_id = $this->userInfo->kefu->user_id;
            $find = $model->where('kefu_id', $kefu_id)
                ->lock(true)
                ->where('create_time', date('Y-m-d'))
                ->find();
            if (!empty($find->kefu_id)) {
                $find->save([
                    'pay_click' => bcadd($find->pay_click,1)
                ]);
            }else{
                $model->save([
                    'kefu_id' => $kefu_id,
                    'create_time' => date('Y-m-d'),
                    'pay_click' => 1
                ]);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
        }
        return $this->msg();
    }

    public function kefu_click(){
        try {
            Db::startTrans();
            $model = new AppKefuClick();
            $kefu_id = $this->userInfo->kefu->user_id;
            $find = $model->where('kefu_id', $kefu_id)
                ->lock(true)
                ->where('create_time', date('Y-m-d'))
                ->find();
            if (!empty($find->kefu_id)) {
                $find->save([
                    'click' => bcadd($find->click,1)
                ]);
            }else{
                $model->save([
                    'kefu_id' => $kefu_id,
                    'create_time' => date('Y-m-d'),
                    'click' => 1
                ]);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
        }
        return $this->msg();
    }

    public function is_click_kefu(){
        $this->userInfo->is_click_kefu = 1;
        $this->userInfo->save();
        return $this->msg();
    }
}